package com.cmsz.shuang.booter.web;

import javax.validation.Valid;
import javax.validation.constraints.NotNull;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;

import com.cmsz.shuang.booter.dao.AccessMapper;
import com.cmsz.shuang.booter.entity.Customer;
import com.cmsz.shuang.booter.model.UserPo;
import com.cmsz.shuang.booter.repository.CustomerRepository;
import com.vaadin.annotations.Push;
import com.vaadin.annotations.Theme;
import com.vaadin.data.util.BeanItemContainer;
import com.vaadin.server.FontAwesome;
import com.vaadin.server.VaadinRequest;
import com.vaadin.spring.annotation.SpringUI;
import com.vaadin.ui.Button;
import com.vaadin.ui.Grid;
import com.vaadin.ui.HorizontalLayout;
import com.vaadin.ui.Notification;
import com.vaadin.ui.TextField;
import com.vaadin.ui.UI;
import com.vaadin.ui.VerticalLayout;


@SpringUI
@Theme("valo")
public class VaadinUI extends UI {
	
	
	@Autowired
	@NotNull
	@Valid
	private AccessMapper access;
	
//	private Grid grid = new Grid();
	
//	CustomerRepository repo;
//	Grid grid;
	
	private final CustomerRepository repo;

	private final CustomerEditor editor;

	private final Grid grid;

	private final TextField filter;

	private final Button addNewBtn;

	
	
	
	@Autowired
	public VaadinUI(CustomerRepository repo, CustomerEditor editor) {
		this.repo = repo;
		this.editor = editor;
		this.grid = new Grid();
		this.filter = new TextField();
		this.addNewBtn = new Button("New customer", FontAwesome.PLUS);
	}
	
    @Override
    protected void init(VaadinRequest request) {
    	
//    	grid.setContainerDataSource(new BeanItemContainer(UserPo.class, access.getAllUserPo()));
    	
//    	setContent(grid);
    	
//	    setContent(new Button("Click me", e->Notification.show("Hello Spring+Vaadin user!")));
    	
//        setContent(grid);
//        listCustomers();
    	
    	// build layout
    			HorizontalLayout actions = new HorizontalLayout(filter, addNewBtn);
    			VerticalLayout mainLayout = new VerticalLayout(actions, grid, editor);
    			setContent(mainLayout);

    			// Configure layouts and components
    			actions.setSpacing(true);
    			mainLayout.setMargin(true);
    			mainLayout.setSpacing(true);

    			grid.setHeight(300, Unit.PIXELS);
    			grid.setColumns("id", "firstName", "lastName");

    			filter.setInputPrompt("Filter by last name");

    			// Hook logic to components

    			// Replace listing with filtered content when user changes filter
    			filter.addTextChangeListener(e -> listCustomers(e.getText()));

    			// Connect selected Customer to editor or hide if none is selected
    			grid.addSelectionListener(e -> {
    				if (e.getSelected().isEmpty()) {
    					editor.setVisible(false);
    				}
    				else {
    					editor.editCustomer((Customer) grid.getSelectedRow());
    				}
    			});

    			// Instantiate and edit new Customer the new button is clicked
    			addNewBtn.addClickListener(e -> editor.editCustomer(new Customer("", "")));

    			// Listen changes made by the editor, refresh data from backend
    			editor.setChangeHandler(() -> {
    				editor.setVisible(false);
    				listCustomers(filter.getValue());
    			});

    			// Initialize listing
    			listCustomers(null);
    	
    	
    }
    
    private void listCustomers() {
        grid.setContainerDataSource(
                new BeanItemContainer(Customer.class, repo.findAll()));
    }
    
    private void listCustomers(String text) {
        if (StringUtils.isEmpty(text)) {
            grid.setContainerDataSource(
                    new BeanItemContainer(Customer.class, repo.findAll()));
        }
        else {
            grid.setContainerDataSource(new BeanItemContainer(Customer.class,
                    repo.findByLastNameStartsWithIgnoreCase(text)));
        }
    }


}
